package createpattern.factory;

/**
 * 工厂类
 *
 * @author seer
 * @date 2018/2/5 11:33
 */
public class ShapeFactory {

    public Shape getShape(String type) {
        if (type == null) {
            return null;
        }
        if ("Circle".equalsIgnoreCase(type)) {
            return new Circle();
        }
        if ("Rectangle".equalsIgnoreCase(type)) {
            return new Rectangle();
        }
        if ("Square".equalsIgnoreCase(type)) {
            return new Square();
        }
        return null;
    }

    /**
     * 反射方式获取，泛型，支持多态
     *
     * @param claszz
     * @param <T>
     * @return
     */
    public static <T> T getInstance(Class<? extends T> claszz) {
        T obj;
        try {
            obj = (T) Class.forName(claszz.getName()).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return obj;
    }
}
